﻿using BaoGiaVeThang.ServiceFlight;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaoGiaVeThang
{
    public class VJCrawler : AirLineCrawler
    {
        public VJCrawler(int startDay)
            : base(startDay)
        {

        }

        public override async void UpdateCheapTicketPrice()
        {
            if (domesticRoute.IsVJ)
            {
                DateTime departTime = getDate();
                try
                {
                    CrawlerClient client = new CrawlerClient();
                    RootMinPrice root = await client.GetPriceInFareVietJetAirAsync(1, domesticRoute.ArrivalAirport, 0, departTime, domesticRoute.DepartureAirport,
                        0, false, departTime.AddDays(10));
                        UpdateToDatabase(root, false, true, false, domesticRoute);
                }
                catch (Exception ex)
                {
                    countError++;
#if DEBUG
                    File.AppendAllText("ErrorVJ.txt", "Depart=" + domesticRoute.DepartureAirport + ";Arrival=" + domesticRoute.ArrivalAirport
                       + ";Date=" + departTime + "\n");
#endif
                }
            }
            Next();
        }
        public override bool isDone()
        {
            int k = MonthInfo.startMonth + 6;
            if (k > 12)
                k = k % 12;
            return monthInfo.month == k;
        }
        public override DateTime getDate()
        {
            int year = DateTime.Now.Year;
            if (DateTime.Now.Month > monthInfo.month)
            {
                year++;
            }
            return new DateTime(year, monthInfo.month, monthInfo.startDay);
        }
    }
}
